package com.example.lib_mvp.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * coloredCircleView.setColor(Color.rgb(255, 0, 0)); // 设置为红色
 */
public class CircleView extends View {

    private Paint paint;
    private int color = Color.RED; // 默认颜色为红色

    public CircleView(Context context) {
        super(context);
        init();
    }

    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        // 设置画笔颜色为RGB值，这里设置为红色
        paint.setColor(color);
        // 设置填充样式为填充内部
        paint.setStyle(Paint.Style.FILL);
    }

    public void setColor(int color) {
        this.color = color;
        paint.setColor(this.color);
        invalidate(); // 重绘View
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 画圆的中心点为View的中心
        int cx = getWidth() / 2;
        int cy = getHeight() / 2;
        // 圆的半径为最小边的一半
        int radius = Math.min(cx, cy);
        // 画圆
        canvas.drawCircle(cx, cy, radius, paint);
    }
}